import ElevatorClass
import Tkinter
import time
import sched
import thread
import array

class GUI(Tkinter.Tk):
    def __init__(GUI,parent):
        Tkinter.Tk.__init__(GUI,parent)
        GUI.first = ElevatorClass.ElevatorClass(elevatorNum=1)
        GUI.first.mutateTop(20)
        GUI.first.mutateBottom(0)
        GUI.first.mutateFloor(0)
        GUI.first.locY = (GUI.first.viewTop()-GUI.first.viewFloor())*10
        GUI.first.mutateDefault(10)

        GUI.americanCheckboxState = 0

        GUI.buildingFloors = (GUI.first.viewTop() - GUI.first.viewBottom())
            
        GUI.initialize()

    def initialize(GUI):
        GUI.grid()
        
        GUI.GUIImageUpdate()

        GUI.grid_columnconfigure(0,weight=1)
        GUI.resizable(True,True)
        GUI.update()
        GUI.geometry(GUI.geometry())

    #Function for updating the GUI
    def GUIImageUpdate(GUI):
        GUI.floorNamingCheckbox = Tkinter.Checkbutton(GUI, text="American?", anchor="w", command=lambda:GUI.FloorNumberingToggle(int(GUI.americanCheckboxState)),\
                                                      variable=GUI.americanCheckboxState, onvalue=1, offvalue=0)
        GUI.floorNamingCheckbox.grid(column=3, row=2, sticky="W")
        
        GUI.topFloorLabelOutput = Tkinter.StringVar()
        GUI.bottomFloorLabelOutput = Tkinter.StringVar()
        GUI.defaultFloorLabelOutput = Tkinter.StringVar()
        GUI.currentFloorLabelOutput = Tkinter.StringVar()
        GUI.topFloorLabelOutput.set("Current Top Floor: " + str(GUI.first.viewTop()))
        GUI.bottomFloorLabelOutput.set("Current Bottom Floor: " + str(GUI.first.viewBottom()))
        GUI.defaultFloorLabelOutput.set("Current Default Floor: " + str(GUI.first.viewDefault()))
        GUI.currentFloorLabelOutput.set("Current Floor: " + str(GUI.first.viewFloor()))
        
        topFloorLabel = Tkinter.Label(GUI,textvariable=GUI.topFloorLabelOutput, anchor="w",fg="black")
        topFloorLabel.grid(column=4,row=20,columnspan=10,sticky='EW')
        bottomFloorLabel = Tkinter.Label(GUI,textvariable=GUI.bottomFloorLabelOutput, anchor="w",fg="black")
        bottomFloorLabel.grid(column=4,row=21,columnspan=10,sticky='EW')
        defaultFloorLabel = Tkinter.Label(GUI,textvariable=GUI.defaultFloorLabelOutput, anchor="w",fg="black")
        defaultFloorLabel.grid(column=4,row=22,columnspan=10,sticky='EW')
        
        GUI.currentFloorInput = Tkinter.StringVar()
        GUI.currentFloorTextbox = Tkinter.Entry(GUI,textvariable=GUI.currentFloorInput)
        GUI.currentFloorTextbox.grid(column=3,row=1,sticky='W')
        GUI.currentFloorTextbox.bind("<Return>", GUI.OnPressEnter)
        GUI.currentFloorInput.set(GUI.first.viewFinalFloor())
        currentFloorNumber = GUI.first.viewFloor()
        currentFloorLabel = Tkinter.Label(GUI,text="Current Floor: " + str(currentFloorNumber), anchor="w",fg="black")
        currentFloorLabel.grid(column=4, row=1)

        currentFloorInt = GUI.first.viewFloor()
        currentFloorButton = Tkinter.Button(GUI, text="Go To Floor", command=lambda:GUI.CallElevatorFunction(int(GUI.currentFloorTextbox.get())))
        currentFloorButton.grid(column=2, row=1)

        topFloorIDLabel = Tkinter.Label(GUI,text="Top Floor", anchor="w",fg="black")
        topFloorIDLabel.grid(column=2, row=20)
        GUI.topFloorInput = Tkinter.StringVar()
        GUI.topFloorTextbox = Tkinter.Entry(GUI,textvariable=GUI.topFloorInput)
        GUI.topFloorTextbox.grid(column=3,row=20,sticky='W')
        GUI.topFloorTextbox.bind("<Return>", GUI.OnPressEnter)
        GUI.topFloorInput.set(GUI.first.viewTop())

        bottomFloorIDLabel = Tkinter.Label(GUI,text="Bottom Floor", anchor="w",fg="black")
        bottomFloorIDLabel.grid(column=2, row=21)
        GUI.bottomFloorInput = Tkinter.StringVar()
        GUI.bottomFloorTextbox = Tkinter.Entry(GUI,textvariable=GUI.bottomFloorInput)
        GUI.bottomFloorTextbox.grid(column=3,row=21,sticky='W')
        GUI.bottomFloorTextbox.bind("<Return>", GUI.OnPressEnter)
        GUI.bottomFloorInput.set(GUI.first.viewBottom())

        defaultFloorIDLabel = Tkinter.Label(GUI,text="Default Floor", anchor="w",fg="black")
        defaultFloorIDLabel.grid(column=2, row=22)
        GUI.defaultFloorInput = Tkinter.StringVar()
        GUI.defaultFloorTextbox = Tkinter.Entry(GUI,textvariable=GUI.defaultFloorInput)
        GUI.defaultFloorTextbox.grid(column=3,row=22,sticky='W')
        GUI.defaultFloorTextbox.bind("<Return>", GUI.OnPressEnter)
        GUI.defaultFloorInput.set(GUI.first.viewDefault())

        GUI.updateFloorInfoButton = Tkinter.Button(GUI, text="Apply", command=lambda:GUI.UpdateFloorInfo())
        GUI.updateFloorInfoButton.grid(column=3, row=23)
                
        #Elevator Image Creation
        imageArray = []
        imageLabelArray = []
        for i in xrange(int(GUI.first.viewBottom()), int(GUI.first.viewTop())+1):
            if GUI.first.viewFloor() == i and GUI.first.viewFinalFloor() == i:
                imageArray.insert(i, Tkinter.PhotoImage(file="elevatorOpen.gif"))
                imageLabelArray.insert(i, Tkinter.Label(GUI, image=imageArray[i]))
                imageLabelArray[i].photo = imageArray[i]
                imageLabelArray[i].grid(column=0,row=(int(GUI.first.viewTop()) + 1 - i))
            elif GUI.first.viewFloor() == i and GUI.first.viewFinalFloor() != i:
                imageArray.insert(i, Tkinter.PhotoImage(file="elevatorTravelling.gif"))
                imageLabelArray.insert(i, Tkinter.Label(GUI, image=imageArray[i]))
                imageLabelArray[i].photo = imageArray[i]
                imageLabelArray[i].grid(column=0,row=(int(GUI.first.viewTop()) + 1 - i))
            else:
                imageArray.insert(i, Tkinter.PhotoImage(file="elevatorClosed.gif"))
                imageLabelArray.insert(i, Tkinter.Label(GUI, image=imageArray[i]))
                imageLabelArray[i].photo = imageArray[i]
                imageLabelArray[i].grid(column=0,row=(int(GUI.first.viewTop()) + 1 - i))
        
    def UpdateFloorInfo(GUI):
        if GUI.topFloorTextbox.get() != None:
            mutateTopValue = int(GUI.topFloorTextbox.get())
            GUI.first.mutateTop(mutateTopValue)
        if GUI.bottomFloorTextbox.get() != None:
            mutateBottomValue = int(GUI.bottomFloorTextbox.get())
            GUI.first.mutateBottom(mutateBottomValue)
        if GUI.defaultFloorTextbox.get() != None:
            mutateDefaultValue = int(GUI.bottomFloorTextbox.get())
            GUI.first.mutateDefault(mutateDefaultValue)
        GUI.GUIImageUpdate()

    def CallElevatorFunction(GUI, x=1):
        if x <= GUI.first.viewTop() and x >= GUI.first.viewBottom():
            if GUI.first.floor <= x:
                floorDiff = x - GUI.first.viewFloor()
            elif GUI.first.viewFloor() > x:
                floorDiff = GUI.first.floor - x
            while GUI.first.viewFloor() != x:
                GUI.first.callToFloor(x)
                GUI.GUIImageUpdate()
                GUI.update()
                time.sleep(1)
        GUI.currentFloorTextbox.focus_set()
        GUI.currentFloorTextbox.selection_range(0, Tkinter.END)

    def FloorNumberingToggle(GUI, american):
        if GUI.first.viewFloorNaming() != american:
            if american == 1:
                GUI.first.mutateTop(int(GUI.first.viewTop())+1)
                GUI.first.mutateBottom(int(GUI.first.viewBottom())+1)
                GUI.first.mutateDefault(int(GUI.first.viewDefault())+1)
                GUI.first.mutateFloor(int(GUI.first.viewFloor())+1)
                GUI.first.mutateFloorNaming(american)
            else:
                GUI.first.mutateTop(int(GUI.first.viewTop())-1)
                GUI.first.mutateBottom(int(GUI.first.viewBottom())-1)
                GUI.first.mutateDefault(int(GUI.first.viewDefault())-1)
                GUI.first.mutateFloor(int(GUI.first.viewFloor())-1)
                GUI.first.mutateFloorNaming(american)
        GUI.GUIImageUpdate()
        
    def OnPressEnter(GUI, event):
        GUI.CallElevatorFunction(int(GUI.currentFloorTextbox.get()))
        GUI.currentFloorTextbox.focus_set()
        GUI.currentFloorTextbox.selection_range(0, Tkinter.END)


if __name__ == "__main__":
    app = GUI(None)
    app.title('CP2013 Elevator Simulator')
    app.mainloop()
